﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace QuanLyNhanVien
{
    public partial class frmNhapHoSo : Form
    {
        public frmNhapHoSo()
        {
            InitializeComponent();
   
           // LayDuLieuQuocTich();
            //LayDuLieuTonGiao();
            LayDuLieuChucVu();
           // LayDuLieuPhongBan();
   
        }

        private void LayDuLieuChucVu()
        {
            int iNumRows = 0;
            int iLen = 0;
            int iIndex = 0;

            byte[] abDataReturn ;
            abDataReturn = Program.DBQuery("OrganizationProfiles", "SELECT DISTINCT m_Name FROM PositionEnum");
            iNumRows = BitConverter.ToInt32(abDataReturn, 0);
            iIndex = 19;
            for (int i = 0; i < iNumRows; i++)
            {
                iLen = BitConverter.ToInt32(abDataReturn, iIndex);
                cmbChucVu.Items.Add(Encoding.UTF8.GetString(abDataReturn, iIndex + 4, iLen));
                iIndex += iLen + 4;
            }
            cmbChucVu.Items.Add("Thêm mới...");
        }

        private void LayDuLieuQuocTich()
        {
            int iNumRows = 0;
            int iLen = 0;
            int iIndex = 0;

            byte[] abDataReturn;
            abDataReturn = Program.DBQuery("POAddress", "SELECT DISTINCT m_Name FROM Nation");
            iNumRows = BitConverter.ToInt32(abDataReturn, 0);
            iIndex = 19;
            for (int i = 0; i < iNumRows; i++)
            {
                iLen = BitConverter.ToInt32(abDataReturn, iIndex);
                cmbQuocTich.Items.Add(Encoding.UTF8.GetString(abDataReturn, iIndex + 4, iLen));
                iIndex += iLen + 4;
            }
            cmbQuocTich.Items.Add("Thêm mới...");

        }

        private void LayDuLieuTonGiao()
        {
            int iNumRows = 0;
            int iLen = 0;
            int iIndex = 0;

            byte[] abDataReturn;
            abDataReturn = Program.DBQuery("EmployeeProfiles", "SELECT DISTINCT Religion FROM Employees");
            iNumRows = BitConverter.ToInt32(abDataReturn, 0);
            iIndex = 21;
            for (int i = 0; i < iNumRows; i++)
            {
                iLen = BitConverter.ToInt32(abDataReturn, iIndex);
                cmbTonGiao.Items.Add(Encoding.UTF8.GetString(abDataReturn, iIndex + 4, iLen));
                iIndex += iLen + 4;
            }
            cmbTonGiao.Items.Add("Thêm mới...");

        }

        private void LayDuLieuPhongBan()
        {
            int iNumRows = 0;
            int iLen = 0;
            int iIndex = 0;

            byte[] abDataReturn;
            abDataReturn = Program.DBQuery("EmployeeProfiles", "SELECT DISTINCT m_Name FROM Departments");
            iNumRows = BitConverter.ToInt32(abDataReturn, 0);
            iIndex = 19;
            for (int i = 0; i < iNumRows; i++)
            {
                iLen = BitConverter.ToInt32(abDataReturn, iIndex);
                cmbPhongBan.Items.Add(Encoding.UTF8.GetString(abDataReturn, iIndex + 4, iLen));
                iIndex += iLen + 4;
            }
            cmbPhongBan.Items.Add("Thêm mới...");
        }    
                
       
        private void txtHoten_Leave(object sender, EventArgs e)
        {
            if (txtHoten.Text == "")
            {
                lblCheckHoten.Text = "(*) Bạn chưa nhập Họ và tên";                
            }
            else 
            {
                lblCheckHoten.Visible = false;
                DateNgaySinh.Focus();                                
            }            
        }

        //Ham Kiem tra tinh hop le so ĐT
        public bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }

        private void txtPhone_Leave(object sender, EventArgs e)
        {
            if (txtPhone.Text == "")
            {
                lblCheckPhone.Text = " (*) Bạn chưa nhập số điện thoại";                
            }
            else
            {
                if (!IsNumber(txtPhone.Text.Trim()))
                {
                    lblCheckPhone.Text = "(*) Số điện thoại phải nhập bằng số";                    
                }
                else
                {
                    lblCheckPhone.Visible = false;
                    txtEmail.Focus();
                }
            }
        }

        //Ham kiem tra tinh hop le cua Email       
        public bool IsValidEmail(string email)
        {
            try
            {
                int nFirt = int.Parse(email.Substring(0, 1));
                return false;
            }
            catch
            {
                string pattern = @"^[_a-zA-Z0-9][_.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[_.a-zA-Z0-9]+)*\.
                (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|vn|[a-zA-Z]{2})$";
                //Regular expression object
                Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);               
                //boolean variable to return to calling method               
                bool valid = false;
                //make sure an email address was provided
                if (string.IsNullOrEmpty(email))
                {
                    valid = false;
                }
                else
                {
                    //use IsMatch to validate the address
                    valid = check.IsMatch(email.ToLower());
                }
                //return the value to the calling method
                return valid;
            }
        }
        private void txtCheckEmail_Leave(object sender, EventArgs e)
        {
           
                if (!IsValidEmail(txtEmail.Text.Trim()))
                {
                    lblCheckEmail.Text = "(*) Email phải có dạng 'name@Domain',VD:software@gmail.com";                    
                }
                else
                {
                    lblCheckEmail.Visible = false;
                    cmbChucVu.Focus();
                }
            
        }

       // Click vào Thoát
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }
        // Click vào Xóa trắng
        private void btnCancel_Click(object sender, EventArgs e)
        {            
            txtHoten.Text = "";
            DateNgaySinh.Text="";
            cmbGioiTinh.Text = "";
            cmbQuocTich.Text = "";
            cmbTonGiao.Text = "";
            cmbHonNhan.Text = "";
            txtThuongTru.Text = "";
            txtTamTru.Text = "";
            txtPassport.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
            cmbChucVu.Text = "";
            cmbPhongBan.Text = "";
            cmbChiNhanhCty.Text = "";
            DateNgayVaoCty.Text = "";
            txtHopDong.Text = "";
            txtBankAcount.Text = "";
            cmbBankName.Text = "";
            cmbBankBranch.Text = "";
            lblCheckPhone.Visible = false;
            lblCheckEmail.Visible = false;
            lblCheckHoten.Visible = false;
            txtHoten.Focus();
            //this.IsNumber(txtHoten.Text);
            //this.IsValidEmail(txtEmail.Text);            
        }      

        private void frmNhapHoSo_Load(object sender, EventArgs e)
        {
            txtHoten.Focus();
        }

        private void cmbQuocTich_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbQuocTich.SelectedItem.ToString() == "Thêm mới...")
            {
                frmTaomoiQuocTich frmTaomoiQuocTich = new frmTaomoiQuocTich();
                frmTaomoiQuocTich.Show();                
            }
        }

        private void cmbTonGiao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbTonGiao.SelectedItem.ToString() == "Thêm mới...")
            {
                frmTaomoiTonGiao frmTaomoiTonGiao = new frmTaomoiTonGiao();
                frmTaomoiTonGiao.Show();
            }

        }

        private void cmbChucVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbChucVu.SelectedItem.ToString() == "Thêm mới...")
            {
                frmTaoMoiChucVu frmTaoMoiChucVu = new frmTaoMoiChucVu();
                frmTaoMoiChucVu.Show();
            }
        }

        private void cmbPhongBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbPhongBan.SelectedItem.ToString() == "Thêm mới...")
            {
                frmTaoMoiPhongBan frmTaoMoiPhongBan = new frmTaoMoiPhongBan();
                frmTaoMoiPhongBan.Show();
            }
        }

        private void cmbChiNhanhCty_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cmbChiNhanhCty.SelectedItem.ToString()=="Thêm mới...")
            {
                frmTaoMoiChiNhanhCty frmTaoMoiChiNhanhCty = new frmTaoMoiChiNhanhCty();
                frmTaoMoiChiNhanhCty.Show();
            }
        }

        private void txtPassport_Leave(object sender, EventArgs e)
        {
            if (txtPassport.Text.ToString() == "")
            { 
                  
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string strCmd;

            strCmd = "INSERT INTO employees()"; 
            Program.DBExecute("EmployeeProfiles", strCmd);
        }    
         
                         
    }
}